﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms.Components {
    /// <summary>
    /// 有效期
    /// </summary>
    class ValidityPeriod {

        private bool isGroup = false;
        /// <summary>
        /// 是否为7位绝对时间
        /// </summary>
        public bool IsGroup {
            get { return isGroup; }
            set { isGroup = value; }
        }

        private int second = 0;
        /// <summary>
        /// 秒数
        /// </summary>
        public int Second {
            get { return second; }
            set { second = value; }
        }

        private DateTime targetTime;
        /// <summary>
        /// 绝对时间
        /// </summary>
        public DateTime TargetTime {
            get { return targetTime; }
            set { targetTime = value; }
        }

        /// <summary>
        /// 设置有效时间
        /// </summary>
        /// <param name="pdutype"></param>
        /// <param name="data"></param>
        public void SetVP(ProtocolDataUnit pdutype, string data) {
            if (pdutype.Vpf == PduTypeVPF.INTEGER) {
                int len = Util.HexString2Oct(data);
                if (len >= 0) {
                    //5分钟为一个周期
                    this.second = (len + 1) * 5;
                }
                if (len >= 144) {
                    //以小时为周期
                    this.second = (len - 143) * 30 + 43200;
                }
                if (len >= 168) {
                    //以天为周期
                    this.second = (len - 167) * 86400;
                }
                if (len >= 197) {
                    //以周为周期
                    this.second = (len - 196) * 604800;
                }
                this.isGroup = false;
            } else if (pdutype.Vpf == PduTypeVPF.GROUP) {
                //绝对的
                this.isGroup = true;
                data = Util.ParityChange(data);

                string year = data.Substring(0, 2);
                string month = data.Substring(2, 2);
                string day = data.Substring(4, 2);
                string hour = data.Substring(6, 2);
                string second = data.Substring(8, 2);
                string minit = data.Substring(10, 2);
                string zone = data.Substring(12, 2);
                this.targetTime = DateTime.Parse(string.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, second, minit));


            }
        }


        public string GetVP() {
            if (!this.isGroup) {
                //转换秒
                if (this.second < 43200) {
                    return string.Format("{0:x}", this.second).PadLeft(2, '0');
                }
                if (this.second < 86400) {
                    int chi = (this.second - 43200) / 30 + 143;
                    return string.Format("{0:x}", chi).PadLeft(2, '0').ToUpper();

                }
                if (this.second < 604800) {
                    int chi = (this.second / 86400) + 167;
                    return string.Format("{0:x}", chi).PadLeft(2, '0').ToUpper();

                }
                if (this.second >= 604800) {
                    int chi = (this.second / 86400 / 7) + 196;
                    return string.Format("{0:x}", chi).PadLeft(2, '0').ToUpper();
                }

                return "FF";
            } else {
                string d = this.targetTime.ToString("yyMMddHHmmss");
                string zone = this.targetTime.ToString("zz");
                zone = Util.Otc2HexString(Int32.Parse(zone));
                return Util.ParityChange(d) + zone;
            }



        }
    }
}
